home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
MacHack 1996
/
MacHack 1996.toast
/
Hacks
/
Hacks ’87
/
Source ƒ.sit
/
Source ƒ
/
C ƒ
/
CITADEL BBS 'C' SRC
/
NETRCV.C
< prev
next >
Wrap
C/C++ Source or Header
|
1987-01-14
|
16KB
|
545 lines
/************************************************************************/
/* netrcv.c */
/* */
/* Networking functions for reception. */
/************************************************************************/
/************************************************************************/
/* history */
/* */
/* 86Aug20 HAW History not maintained due to space problems. */
/************************************************************************/
#include "ctdl.h"
/************************************************************************/
/* contents */
/* */
/************************************************************************/
/************************************************************************/
/* External variable declarations in NET.C */
/************************************************************************/
char processMail;
#ifdef NET_BUG
extern FILE *localcheck;
#endif
extern FILE *netLog, *netMisc;
extern ulong roomHiMsgs[MAXROOMS];
extern unsigned char sectBuf[SECTSIZE + 5];
extern int counter;
extern int callSlot;
extern char checkNegMail;
extern char inReceive;
extern int FinHour, FinMinute;
extern label normed, callerName, callerId;
char normId(), getNetMessage();
char callOut();
unsigned char inp();
char *netRoomTemplate = "%c:room%d.$$$";
extern struct nodeRoomsTab *sharedRooms;
extern struct config cfg; /* Lots an lots of variables */
extern struct logBuffer logBuf; /* Person buffer */
extern struct aRoom roomBuf; /* Room buffer */
extern struct rTable roomTab[];
extern struct msgB msgBuf,tempMess;
extern struct netBuffer netBuf;
extern struct netTable *netTab;
extern FILE *upfd;
extern int thisNet;
extern char onConsole;
extern char loggedIn; /* Is we logged in? */
extern char outFlag; /* Output flag */
extern char haveCarrier; /* Do we still got carrier? */
extern unsigned char modStat; /* Needed so we don't die */
extern char usingWCprotocol;/* WC flag */
extern char WCError;
extern int thisRoom;
extern int thisLog;
extern char *dotdot;
extern char *confirm;
/************************************************************************/
/* External function definitions for NET.C */
/************************************************************************/
FILE *safeopen();
FILE *fopen();
char *realloc();
char *index();
long getNumber();
/************************************************************************/
/* called() We've been called, so let's handle it */
/************************************************************************/
called()
{
int count;
int yr, hr, min, dy;
char *mn;
inReceive = TRUE;
getdate(&yr, &mn, &dy, &hr, &min);
splitF(netLog, "Caller detected (%d:%02d)\n", hr, min);
processMail = checkNegMail = FALSE;
if (!called_stabilize()) return ;
splitF(netLog, "Caller stabilized\n");
getId();
if (!haveCarrier) return;
rcvStuff(FALSE);
splitF(netLog, "Networking finished, terminating call from %s\n\n",
callerName);
modStat = haveCarrier = FALSE;
interpret(cfg.pHangUp);
doResults();
}
/************************************************************************/
/* rcvStuff() receive stuff, like it says. */
/************************************************************************/
rcvStuff(reversed)
char reversed;
{
label tempNm;
struct cmd_data cmds;
zero_struct(cmds);
do {
getNextCommand(&cmds);
switch (cmds.command) {
case HANGUP: break;
case NORMAL_MAIL: getMail(&cmds); break;
case A_FILE_REQ:
case R_FILE_REQ: netFileReq(&cmds); break;
case NET_ROOM: netRoomReq(&cmds); break;
case ROLE_REVERSAL: reqReversal(&cmds, reversed); break;
case CHECK_MAIL: reqCheckMail(&cmds); break;
default: sPrintf(tempNm, "'%d' unknown.", cmds.command);
reply(BAD, tempNm); break;
}
} while (gotCarrier() && cmds.command != HANGUP);
}
/************************************************************************/
/* doResults() processes results of receiving thingies and such. */
/************************************************************************/
doResults()
{
int inMail();
if (processMail) readMail(TRUE, inMail);
if (callSlot == ERROR) /* If didn't know this node, don't */
return ; /* bother with anything else */
if (checkNegMail) readNegMail();
doNetRooms();
putNet(thisNet);
}
/************************************************************************/
/* getId() Gets nodeId and nodeName from caller. */
/************************************************************************/
getId()
{
int increment();
int i;
char *secRunner;
if (!haveCarrier) return;
counter = 0;
readFile(increment);
if (!gotCarrier()) return modStat = haveCarrier = FALSE;
strCpy(callerId, sectBuf);
secRunner = sectBuf;
while (*secRunner != 0) secRunner++;
secRunner++;
strCpy(callerName, secRunner);
normId(callerId, normed);
if ((callSlot = searchNet(normed)) == ERROR) {
sPrintf(msgBuf.mbtext, "New caller: %s (%s)", callerName, callerId);
aideMessage(FALSE);
}
else dumpNodeRoom(TRUE);
splitF(netLog, "Call from %s (%s)\n", callerName, callerId);
}
/************************************************************************/
/* getNextCommand() Gets next command from caller */
/************************************************************************/
getNextCommand(cmds)
struct cmd_data *cmds;
{
int increment();
counter = 0;
readFile(increment);
if (!gotCarrier()) return modStat = haveCarrier = FALSE;
grabCommand(cmds, sectBuf);
}
/************************************************************************/
/* grabCommand() Pulls network cmds out of specified buffer */
/************************************************************************/
grabCommand(cmds, sect)
char *sect;
struct cmd_data *cmds;
{
int fcount = 0;
cmds->command = sect[0];
sect++;
while (sect[0] > 0 && fcount < 4) {
strCpy(cmds->fields[fcount++], sect);
while (*sect != 0) sect++;
sect++;
}
}
/************************************************************************/
/* reply() Replies to caller */
/************************************************************************/
reply(state, reason)
char state;
char *reason;
{
if (!doWC(STARTUP)) {
no_good("Couldn't send reply to %s!", TRUE);
modStat = haveCarrier = FALSE;
interpret(cfg.pHangUp);
return;
}
sendWCChar(state);
if (state == BAD) {
mWCprintf("%s", reason);
if (cfg.debug) splitF(netLog, "Replying BAD: %s\n", reason);
}
sendWCChar(0);
doWC(FINISH);
}
/************************************************************************/
/* reqReversal() reverses roles of networking */
/************************************************************************/
reqReversal(cmds, reversed)
char reversed;
struct cmd_data *cmds;
{
splitF(netLog, "Role reversal\n");
if (reversed) {
reply(BAD, "Synch error on Reversal!");
return ;
}
reply(GOOD, "");
if (callSlot == ERROR) /* Forces a "null" role reversal */
zero_struct(netBuf.nbflags);
sendStuff(TRUE);
}
/************************************************************************/
/* reqCheckMail() does negative acks on netMail */
/************************************************************************/
reqCheckMail(cmds)
struct cmd_data *cmds;
{
int targetCheck();
if (!processMail) {
reply(BAD, "No mail to check!");
return ;
}
reply(GOOD, "");
splitF(netLog, "checking Mail\n");
if (doWC(STARTUP)) {
readMail(FALSE, targetCheck);
sendWCChar(NO_ERROR);
doWC(FINISH);
}
}
/************************************************************************/
/* targetCheck() checks for existence of recipients */
/************************************************************************/
targetCheck()
{
char sigChar;
int logNo;
struct logBuffer lBuf;
if (!tempMess.mbto[0]) { /* Tsk, tsk!!! */
sigChar = BAD_FORM;
}
else {
logNo = findPerson(tempMess.mbto, &lBuf);
if (logNo == ERROR && hash(tempMess.mbto) != hash("Sysop")) {
sigChar = NO_RECIPIENT;
}
else return ;
}
sendWCChar(sigChar);
mWCprintf(tempMess.mbauth);
mWCprintf(tempMess.mbto);
mWCprintf("%s @ %s", tempMess.mbdate, tempMess.mbtime);
}
/************************************************************************/
/* doNetRooms() integrates temporary files into data base */
/************************************************************************/
doNetRooms()
{
label tempNm;
int rover;
for (rover = 0; rover < SHARED_ROOMS; rover++) {
/*splitF(netLog, "doNetRooms(): %d\n", rover);*/
if (chkNeedsProcessing(rover)) {
/*splitF(netLog, "1\n");*/
sPrintf(tempNm, netRoomTemplate, cfg.netDisk + 'a',
netRoomSlot(rover));
/*splitF(netLog, "2: -%s-\n", tempNm);*/
#ifdef NET_BUG
if ((netMisc = localcheck = safeopen(tempNm, "rb")) == NULL) {
#else
if ((netMisc = safeopen(tempNm, "rb")) == NULL) {
#endif
/*splitF(netLog, "2.a\n");*/
check();
no_good("Couldn't open temproom file from %s.", TRUE);
/*splitF(netLog, "2.b\n");*/
check();
return;
}
/*splitF(netLog, "3\n");*/
check();
getRoom(netRoomSlot(rover));
/*splitF(netLog, "4: thisRoom=%d\n", thisRoom);*/
check();
/*splitF(netLog, "5\n");*/
check();
while (getNetMessage()) {
/*splitF(netLog, "6: loop\n");*/
check();
if (strCmpU(cfg.nodeId + cfg.codeBuf, tempMess.mborig)
!= SAMESTRING) {
/*splitF(netLog, "6.1\n");*/
check();
putNetMessage();
/*splitF(netLog, "6.2\n");*/
check();
noteMessage(NULL, ERROR);
/*splitF(netLog, "6.3\n");*/
check();
}
/*splitF(netLog, "7\n");*/
check();
}
/*splitF(netLog, "8\n");*/
check();
fclose(netMisc);
/*splitF(netLog, "9\n");*/
unlink(tempNm);
/*splitF(netLog, "10\n");*/
resetNeedsProcessing(rover);
/*splitF(netLog, "11\n");*/
}
}
}
#ifdef NET_BUG
check()
{
if (localcheck != netMisc)
splitF(netLog, "DISASTER!\n");
}
#endif
/************************************************************************/
/* getMail() Grabs mail from caller */
/************************************************************************/
getMail(cmds)
struct cmd_data *cmds;
{
label tempNm;
int putFLChar();
splitF(netLog, "Receiving mail from %s\n", callerName);
reply(GOOD, "");
strCpy(tempNm, "a:tempmail.$$$");
tempNm[0] = cfg.netDisk + 'a';
if ((upfd = safeopen(tempNm, "wrb")) == NULL) {
crashout("Couldn't create networker temporary file!");
}
readFile(putFLChar);
fclose(upfd);
processMail = TRUE;
}
/************************************************************************/
/* netFileReq() Handles request for file transfer */
/************************************************************************/
netFileReq(cmds)
struct cmd_data *cmds;
{
int netMultiSend();
int roomSlot;
int data;
char reason[50];
FILE *fd;
splitF(netLog, "\nFile request: %s in %s\n", cmds->fields[1],
cmds->fields[0]);
if ((roomSlot = roomExists(cmds->fields[0])) == ERROR ||
roomTab[roomSlot].rtflags.MSDOSDIR != 1 ||
roomTab[roomSlot].rtflags.DOWNLOAD != 1) {
sPrintf(reason, "Room %s does not exist.", cmds->fields[0]);
if (cfg.debug) splitF(netLog, "roomSlot=%d,DIR=%d,DOWNLOAD=%d\n",
roomSlot,
roomTab[roomSlot].rtflags.MSDOSDIR,
roomTab[roomSlot].rtflags.DOWNLOAD);
reply(BAD, reason);
return;
}
getRoom(roomSlot);
if (cmds->command == A_FILE_REQ) {
reply(GOOD, "");
sPrintf(msgBuf.mbtext, "Following files sent to %s from %s: ",
callerName, roomBuf.rbname);
wildCard(netMultiSend, cmds->fields[1]);
if (doWC(STARTUP)) {
mWCprintf("");
doWC(FINISH);
}
}
else {
if (!setSpace(roomBuf.rbdisk, roomBuf.rbdirname)) {
sPrintf(reason, "Couldn't open directory for %s.", cmds->fields[0]);
reply(BAD, reason);
return;
}
if ((fd = safeopen(cmds->fields[1], "rb")) == NULL) {
sPrintf(reason, "There is no '%s' in %s.", cmds->fields[1],
cmds->fields[0]);
reply(BAD, reason);
if (strLen(roomBuf.rbdirname)!= 0) setSpace(roomBuf.rbdisk, dotdot);
setSpace(cfg.homeDisk, "");
return;
}
reply(GOOD, "");
sendWCFile(fd);
sPrintf(msgBuf.mbtext,
"%s downloaded from %s during networking by %s.",
cmds->fields[1], roomBuf.rbname, callerName);
if (strLen(roomBuf.rbdirname) != 0) setSpace(roomBuf.rbdisk, dotdot);
setSpace(cfg.homeDisk, "");
}
aideMessage(FALSE);
}
/************************************************************************/
/* netRoomReq() Handles request for room networking */
/************************************************************************/
netRoomReq(cmds)
struct cmd_data *cmds;
{
int slot;
char reason[80];
int arraySlot;
if ((slot = roomExists(cmds->fields[0])) == ERROR) {
sPrintf(reason, "'%s' does not exist", cmds->fields[0]);
reply(BAD, reason);
return ;
}
if (roomTab[slot].rtflags.SHARED == 0) {
sPrintf(reason, "'%s' is not a networking room", cmds->fields[0]);
reply(BAD, reason);
return ;
}
if (callSlot == ERROR || (arraySlot = chkSharing(slot)) == ERROR) {
splitF(netLog, "BAD: callSlot=%d, arraySlot=%d\n", callSlot, arraySlot);
sPrintf(reason, "'%s' is not networking with you", cmds->fields[0]);
reply(BAD, reason);
return ;
}
reply(GOOD, "");
sPrintf(reason, netRoomTemplate, cfg.netDisk + 'a', slot);
upfd = safeopen(reason, "wb");
splitF(netLog, "Receiving %s\n", cmds->fields[0]);
readFile(putFLChar);
fclose(upfd);
setNeedsProcessing(arraySlot); /* Set flag indicating room has netted*/
/*splitF(netLog, "After processing %s:\n\n", cmds->fields[0]);*/
dumpNodeRoom(TRUE);
}
/************************************************************************/
/* chkSharing() See if given room slot is networking with system */
/* that has just called */
/************************************************************************/
chkSharing(i)
int i;
{
int rover;
for (rover = 0; rover < SHARED_ROOMS; rover++) {
if (netBuf.netRooms[rover].srgen & 0x8000 &&
netRoomSlot(rover) == i &&
netGen(rover) == roomTab[i].rtgen)
return rover;
}
return ERROR;
}
/************************************************************************/
/* netMultiSend() Send files via WC */
/************************************************************************/
netMultiSend(fn)
char *fn;
{
FILE *fd;
long fseek(), size, Sectors;
if (!gotCarrier()) return ;
if ((fd = safeopen(fn, "rb")) == NULL) {
movmem(&msgBuf, &tempMess, sizeof msgBuf);
sPrintf(msgBuf.mbtext, "Network error: Couldn't find requested file %s",
fn);
movmem(&tempMess, &msgBuf, sizeof msgBuf);
return;
}
strCat(msgBuf.mbtext, fn);
strCat(msgBuf.mbtext, " ");
size = (fseek(fd, -1l, 2)+1);
Sectors = ((size + 127) / SECTSIZE);
rewind(fd);
if (doWC(STARTUP)) {
mWCprintf("%s", fn);
mWCprintf("%ld", Sectors);
doWC(FINISH);
}
sendWCFile(fd);
}